/*
 * Controller test class
 * Authors: Daniel Henderson, Ching-Ting Huang
 * TCSS 360 (Group project) Spring 2013 
 * University of Washington
 */
package View;


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Model.Controller;
import Model.Paper;
import Model.User;

/**
 * This class represents the panel after login that every tab sits on
 * 
 * @author Daniel Henderson, Ching-Ting Huang
 * @version UWTCSS 360, Spring 2013. 3-squared-software-systems
 */
public class ConferencesApp extends JFrame implements Observer{		//can't extend JPanel if extending Observable for logout btn

	private static final long serialVersionUID = 9057758619093026546L;
	private TabPanel tabs;
	private Controller controller;
	private Login loginWindow;
	private JButton logout;
	private JPanel primaryPanel;
	private JLabel name;


	//programFrame.setPreferredSize(new Dimension(800, 800));

	
	/**
	 * Instantiate the main panel and adding the tabbed panel.
	 * 
	 * @param control is the controller in the model that handles all queries.
	 * @param programFrame 
	 */
	public ConferencesApp(final Controller control) {
		primaryPanel = new JPanel();
		name = new JLabel("");
		controller = control;	
		controller.addObserver(this);
		loginWindow = new Login(controller);
		logout = logoutBtn(this);
		logout.setEnabled(false);
		Container loginScreen = wrapComponents();
		Container logoutbtn = btnLayout();
		//containers.setPreferredSize(new Dimension(250, 250));
		
		primaryPanel.add(loginScreen);
		primaryPanel.setPreferredSize(new Dimension(500, 500));
		primaryPanel.setVisible(true);
		
		setTitle("MSEE Conference & Peer-Review Management Software");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		saveOnClosingWindow();
		setPreferredSize(new Dimension(500, 500));
		setLayout(new BorderLayout());
		add(logoutbtn, BorderLayout.NORTH);
		add(primaryPanel, BorderLayout.CENTER);
		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	} //MainPanel

	/**
	 * Save all information before closing the window and the program.
	 */
	private void saveOnClosingWindow() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				if (e.getSource() == ConferencesApp.this) {
					int result = JOptionPane.showConfirmDialog(null, 
							"Are you sure you want to quit the program?", 
							"Exit Program", 
							JOptionPane.YES_NO_OPTION);
					
					if (result == JOptionPane.OK_OPTION) {
						controller.saveData();
						ConferencesApp.this.dispose();
					} 
				}
			} //windowClosing
		});
	} //closeWindow
	
	/**
	 * Sets up logout button.
	 * 
	 * @return the logout button.
	 * @author Ching-Ting Huang
	 * @param conferencesApp 
	 */
	private JButton logoutBtn(final ConferencesApp conferencesApp) {
		final JButton logout = new JButton("Logout");
		logout.addActionListener(new ActionListener() {
		      public void actionPerformed(final ActionEvent the_event) {
		    	  if (the_event.getSource() == logout) {
		    		  controller.setUserExists(false);
		    		  logout.setEnabled(false);
		    		  primaryPanel.setVisible(true);
		  			  loginWindow.setVisible(true);
		  			  tabs.setVisible(false);
		  			  name.setVisible(false);
		  			loginWindow.clearLoginBox();
		  			conferencesApp.setPreferredSize(new Dimension(500, 500));
					primaryPanel.setPreferredSize(new Dimension(500, 500));
					conferencesApp.pack();
					setLocationRelativeTo(null);
		    	  }
		        }
		      });
		return logout;
	} //logoutBtn
	
	/**
	 * Setup placements for components: log-out button at top right of frame.
	 * 
	 * @return the container for the log-out button.
	 * @author Ching-Ting Huang
	 */
	private Container btnLayout() {
		Container top = new JPanel(new FlowLayout(FlowLayout.TRAILING));
		top.add(name);
		top.add(logout);
		return top;
	} //btnLayout
	
	/**
	 * Setup placements for components: log-in window at the center of the frame.
	 * 
	 * @return the container for the login window (borderlayout).
	 * @author Ching-Ting Huang
	 */
	private Container wrapComponents() {	
		Container center = new JPanel(new BorderLayout());
		center.add(loginWindow, BorderLayout.CENTER);
		return center;
	} //wrapComponents
	
	/**
	 * Update method for the primary App class that is used for the Observer pattern.
	 * If Arg1 is the Controller and Arg2 is a User, we know a user was added to
	 * the system, so redraw the panes. If arg2 is a Paper object, we know a
	 * paper was either added or removed from the system, redraw and set some
	 * field values. If Arg2 is an Integer object, redraw the system (this one is
	 * used throughout the controller when data changes, and we just need to redraw the GUI).
	 * 
	 * @return the container for the login window (borderlayout).
	 * @author Daniel Henderson
	 */
	@Override
	public void update(Observable arg0, Object arg1) {

		if(arg0 instanceof Controller && arg1 instanceof User) { //Controller has changed (Add User to System) login window, redraw window and save data
			if(controller.getUserExists()) {
				System.out.println("User was logged into system. Show main program");
				logout.setEnabled(true);
				name.setText("Logged in as: " + controller.getCurrentUsername());
				name.setVisible(true);
				loginWindow.setVisible(false);
				primaryPanel.setVisible(false);
				tabs = new TabPanel(controller);
				tabs.setVisible(true);
				this.add(tabs);
				this.setPreferredSize(new Dimension(1200, 500));
				primaryPanel.setPreferredSize(new Dimension(1200, 500));
				pack();
				setLocationRelativeTo(null);
				repaint();
				controller.saveData();		
			}

		}
		if(arg0 instanceof Controller && arg1 instanceof Paper) { //Controller has changed (Submit Paper) Save files
			System.out.println("Paper was either added or removed. Update Submitted Papers Tab");
			tabs.setVisible(false);
			this.remove(tabs);
			tabs = new TabPanel(controller);
			tabs.setVisible(true);
			this.add(tabs);
			this.setPreferredSize(new Dimension(1200, 500));
			primaryPanel.setPreferredSize(new Dimension(1200, 500));
			pack();
			setLocationRelativeTo(null);
			repaint();
			controller.saveData();	
		}
		if(arg0 instanceof Controller && arg1 instanceof Integer) { //Controller has changed (Changed Current User Permissions)
			Integer onehundred = 100;
			if(onehundred.compareTo((Integer) arg1) == 0) {
				System.out.println("User perms changed, redrawing Tabs");
				name.setText("Logged in as: " + controller.getCurrentUsername());
				tabs.setVisible(false);
				this.remove(tabs);
				tabs = new TabPanel(controller);
				tabs.setVisible(true);
				this.add(tabs);
				this.setPreferredSize(new Dimension(1200, 500));
				primaryPanel.setPreferredSize(new Dimension(1200, 500));
				pack();
				setLocationRelativeTo(null);
				repaint();
				controller.saveData();	
			}

		}
	}
} //class
